package com.xfsg.core.log.enhancer;


import com.xfsg.core.log.config.AliyunLogItem;
import com.xfsg.core.log.config.AliyunLogProperties;

/**
 * Abstract com.xfsg.core.log enhancer
 *
 * @Author wanglaomo
 * @Date 2019/4/4
 **/
public abstract class AbstractLogEnhancer implements LogEnhancer {

    @Override
    public void enhance(AliyunLogProperties config, AliyunLogItem logItem) {

        try {
            if(!hasBeanEnhanced()) {
                doEnhance(config,logItem);
            }
        } catch (Exception e) {
            handlerEnhanceError(e);
        } finally {
            afterEnhance();
        }
    }

    protected abstract boolean hasBeanEnhanced();

    protected abstract void doEnhance(AliyunLogProperties config, AliyunLogItem logItem);

    protected abstract void afterEnhance();

    protected void handlerEnhanceError(Exception exception) {

        cleanUp();
        System.err.println("Failed to add aliyun appender");
        exception.printStackTrace(System.err);
        throw new IllegalStateException(exception);
    }
}
